package com.runachina.java;

import android.text.Selection;
import android.text.Spannable;
import android.util.Log;
import android.widget.EditText;

import com.runachina.common.BaseActivity;
import com.runachina.common.RudeActivity;
import com.runachina.common.RudeRunaModbusActivity;
import com.runachina.common.RudeSimpleActivity;
import com.runachina.othercompany.EuropeCompanyActivity;
import com.runachina.othercompany.HuizhongActivity;
import com.runachina.othercompany.OtherCompanyActivity;
import com.runachina.runamodbus.RunaModusActivity;
import com.runachina.runamodbus.RunaModusNumActivity;
import com.runachina.runa.CheckActivity;
import com.runachina.runa.FlowActivity;
import com.runachina.runa.ModifyNumActivity;
import com.runachina.runa.ModifyTimeActivity;
import com.runachina.runa.ReadMeterActivity;
import com.runachina.runa.SetAttributeActivity;
import com.runachina.runa.SetDataActivity;
import com.runachina.temcollector.TemModifyNumActivity;

/**
 * Created by Isay on 2016/1/29.
 */
public class RcvDataAnalysis {

    /*读取抄表解析：runa188*/
    public static void runaReadMeterRcv(byte[] recv_byte) {

        //1.判断rcv是否符合接收格式 return:p是0x68的位置
        int p = checkRcvData(recv_byte, (byte) 0x68);//判断recv_byte[]中是否包含a 和 b

        //显示所有接收到的字节
        //ReadMeterActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte));
        //显示所有接收到的字节 中 表返回
        ReadMeterActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));

//        FE FE FE FE FE FE FE FE FE FE FE FE FE  前导符
//        68 25 91 09 20 59 00 11 11 81 2E   起始符+仪表类型+地址+控制码+数据长度（0x2E=46）
//        1F 90 12 00 00 00 00 05 00 03 00 00 05 00 00 00 00 17 00 00 00 00 35 27 00 00 00 2C 60 14 00 74 14 00 46 54 00  51 49 09 22 04 10 20 00 00  数据域
//        6D 16 校验和+结束符
        //日期时间
        String dataTime = "";
        dataTime = alldata_analysis(recv_byte, 7, p + 54, 0, 0);
        if (dataTime.length() == 14) {
            dataTime = dataTime.substring(0, 4) + "-" + dataTime.subSequence(4, 6) + "-" + dataTime.subSequence(6, 8) + " "
                    + dataTime.substring(8, 10) + ":" + dataTime.substring(10, 12) + ":" + dataTime.substring(12, 14);
        } else {
            dataTime = alldata_analysis(recv_byte, 7, p + 54, 0, 0);
        }
        if (p > 0) {
            //2.解析表号并显示
            ReadMeterActivity.rcvtext.setText(
                    "表        号:  " + alldata_analysis(recv_byte, 4, p + 5, 0, 0) + "\n"
                            + "冷        量:  " + alldata_analysis(recv_byte, 4, p + 17, 1, 1) + "\n"
                            + "热        量:  " + alldata_analysis(recv_byte, 4, p + 22, 1, 1) + "\n"
                            + "功        率:  " + alldata_analysis(recv_byte, 4, p + 27, 1, 1) + "\n"
                            + "流        速:  " + alldata_analysis(recv_byte, 4, p + 32, 1, 1) + "\n"
                            + "累计流量:  " + alldata_analysis(recv_byte, 4, p + 37, 1, 1) + "\n"
                            + "供水温度:  " + alldata_analysis(recv_byte, 3, p + 41, 0, 1) + "  ℃" + "\n"
                            + "回水温度:  " + alldata_analysis(recv_byte, 3, p + 44, 0, 1) + "  ℃" + "\n"
                            + "工作时间:  " + alldata_analysis(recv_byte, 3, p + 47, 0, 0) + "    h" + "\n"
                            + "表    时间:  " + dataTime + "\n"
                            + "表    状态:  " + alldata_analysis(recv_byte, 2, p + 56, 0, 0) + "\n"
            );
        } else {
            ReadMeterActivity.rcvtext.setText("表未返回数据,如下是调试工具返回!");
        }

    }


    /**
     * 最终目的：找到接收报文中0x68开始的位置
     *
     * @param recv_byte
     * @param a
     * @return
     */
    public static int checkRcvData(byte[] recv_byte, byte a) {
        int p = 0;

        //找转发协议头68位置：68 xx1 yy1 xx2 yy2
        if (p == 0) {
            for (int i = 0; i < recv_byte.length - 24; i++) {
                if ((recv_byte[i] == 0x68)
                        && (recv_byte[i + 1] == recv_byte[i + 3])
                        && (recv_byte[i + 2] == recv_byte[i + 4])
                        && ((recv_byte[i + 1] != 0x00)
                        || (recv_byte[i + 2] != 0x00))) {
                    p = i + 21;
                    i = recv_byte.length;
                }
            }
        }
        //找接收报文首68位置：FE FE 68
        if (p != 0) {
            for (int j = p; j < recv_byte.length - 24; j++) {
                if ((recv_byte[j + 1] == ((byte) 0xFE))
                        && ((recv_byte[j + 2] == ((byte) 0xFE))
                        && ((recv_byte[j + 3] == ((byte) 0x68))))) {
                    p = j + 3;
                    j = recv_byte.length;
                }
            }
        }
        //判断是否只有调试工具返回：68 3E 00 3E 00
        for (int m = 0; m < 8; m++) {
            if ((recv_byte[m] == 0x68)
                    && (recv_byte[m + 1] == 0x3E)
                    && (recv_byte[m + 2] == 0x00))
                p = 0;
        }

        //读取阀门后新添加 返回错误项
//        for (int m = 0; m < 8; m++) {
//            if ((recv_byte[m] == 0x68)
//                    && (recv_byte[m + 1] == 0x42)
//                    && (recv_byte[m + 2] == 0x00))
//                p = 0;
//        }
        return p;
    }

    /**
     * @param many     向前解析字节数
     * @param position 开始解析位置
     * @param unitP    单位位置
     * @param point    是否有小数点需要解析
     */

    public static String alldata_analysis(byte[] recv_byte, int many, int position, int unitP, int point) {
        //解析成字符串P
        String rcvStr = "", transStr;
        try {
            for (int i = 0; i < many; i++) {
                transStr = Integer.toHexString(recv_byte[position - i] & 0xFF).toUpperCase();
                if (transStr.length() == 1) transStr = "0" + transStr;
                rcvStr = rcvStr + transStr;//向前解析
            }
        } catch (Exception e) {
            Log.e("error", "e :" + e.getMessage());
            rcvStr = "error!";
        }
        //插入小数点,先小数点后单位
        if (point != 0) {
            try {
                rcvStr = rcvStr.substring(0, rcvStr.length() - 2) + "." + rcvStr.substring(rcvStr.length() - 2, +rcvStr.length());
            } catch (Exception e) {
                rcvStr = "error!";
            }
        }
        //单位解析，先小数点后单位
        if (unitP != 0) {
            try {
                rcvStr = rcvStr + "  " + unit_analysis(recv_byte[position + unitP]);//-4是单位所在的字节
            } catch (Exception e) {
                rcvStr = "error!";
            }
        }
        return rcvStr;
    }


    /**
     * @param unitbyte 单位字节
     * @return 单位Str
     */
    public static String unit_analysis(byte unitbyte) {
        String unitStr = "";
        switch (unitbyte)//单位  位解析
        {
            case 0x01:
                unitStr = "J";
                break;
            case 0x02:
                unitStr = "wh";
                break;
            case 0x05:
                unitStr = "kwh";
                break;
            case 0x08:
                unitStr = "Mwh";
                break;
            case 0x0A:
                unitStr = "100xMwh";
                break;
            case 0x0B:
                unitStr = "KJ";
                break;
            case 0x0E:
                unitStr = "MJ";
                break;
            case 0x11:
                unitStr = "GJ";
                break;
            case 0x13:
                unitStr = "GJx100";
                break;
            case 0x14:
                unitStr = "W";
                break;
            case 0x17:
                unitStr = "kw";
                break;
            case 0x1A:
                unitStr = "Mw";
                break;
            case 0x29:
                unitStr = "L";
                break;
            case 0x2C:
                unitStr = "m³";
                break;
            case 0x32:
                unitStr = "L/h";
                break;
            case 0x35:
                unitStr = "m³/h";
                break;
        }
        return unitStr;
    }

    /**
     * @param recv_byte 修改表号：接收解析
     */
    public static void runaModifyMeterReadNumRcv(byte[] recv_byte) {
        //判断rcv是否符合接收格式 return:p是0x68的位置
        int p = checkRcvData(recv_byte, (byte) 0x68);//判断recv_byte[]中是否包含a 和 b

        //1.display 接收报文
        ModifyNumActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));
        if (p > 0) {
            //2.display oldnum
            ModifyNumActivity.oldnum.setText(alldata_analysis(recv_byte, 4, p + 5, 0, 0));
            setMousePosition(ModifyNumActivity.oldnum);
            //3.display 接收解析
            ModifyNumActivity.rcvtext.setText("原表号    :" + alldata_analysis(recv_byte, 4, p + 5, 0, 0));
        } else {
            //3.display 接收解析
            ModifyNumActivity.rcvtext.setText("原表号    :" + "表未返回数据,如下是调试工具返回!");
        }
    }

    /**
     * 公共：set mouse position
     */
    private static void setMousePosition(EditText edit) {
        CharSequence text = edit.getText();
        if (text instanceof Spannable) {
            Spannable spanText = (Spannable) text;
            Selection.setSelection(spanText, text.length());
        }
    }


    /**
     * @param recv_byte 修改表号：设置
     *                  返回：FE FE FE 68 3E 00 3E 00 68 88 00 00 01 00...没有返回表号
     */
    public static void runaModifyMeterSetNumRcv(byte[] recv_byte) {
        //判断rcv是否符合接收格式 return:p是0x68的位置
        int p = checkRcvData(recv_byte, (byte) 0x68);//判断recv_byte[]中是否包含a 和 b
        //接收报文
        ModifyNumActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));
        //EditText num
        //ModifyNumActivity.oldnum.setText(alldata_analysis(recv_byte,4, p+5, 0, 0));
        //ModifyNumActivity.oldnum.setText(alldata_analysis(recv_byte,4, p+5, 0, 0));
        //接收解析
        //if(p!=0){
        ModifyNumActivity.rcvtext.setText("新表号    :" + "请确认是否修改成功!");
        ModifyNumActivity.oldnum.setText("");
        //}

        // ModifyNumActivity.rcvtext.setText("新表号    :"+alldata_analysis(recv_byte,4, p+5, 0, 0));
    }

    /**
     * @param recv_byte 重新对时：设置，接收解析
     *                  没有返回时间
     */
    public static void runaModifyTimeSetTimeRcv(byte[] recv_byte) {
        //判断rcv是否符合接收格式 return:p是0x68的位置
        int p = checkRcvData(recv_byte, (byte) 0x68);//判断recv_byte[]中是否包含a 和 b
        //接收报文
        ModifyTimeActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));
        //接收解析
        if (p > 0)
            ModifyTimeActivity.rcvtext.setText("表时间    :" + alldata_analysis(recv_byte, 7, p + 54, 0, 0));
        else
            ModifyTimeActivity.rcvtext.setText("表时间    :" + "请确认是否修改成功!");
    }

    /**
     * 仪表属性:设置接收解析
     */
    public static void SetAttributesetBtn(byte[] recv_byte) {
        //判断rcv是否符合接收格式 return:p是0x68的位置
        int p = checkRcvData(recv_byte, (byte) 0x68);//判断recv_byte[]中是否包含a 和 b
        //接收报文
        SetAttributeActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));

        if (p == 0) {
            SetAttributeActivity.rcvtext.setText("请确认是否修改成功!");
        }
    }

    /**
     * @param recv_byte 数据设置:读取
     */
    public static void SetDataReadBtn(byte[] recv_byte) {
        //判断rcv是否符合接收格式 return:p是0x68的位置
        int p = checkRcvData(recv_byte, (byte) 0x68);//判断recv_byte[]中是否包含a 和 b
        //接收报文
        SetDataActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));
        if (p > 0) {
            //表号
            SetDataActivity.nownum.setText(alldata_analysis(recv_byte, 4, p + 5, 0, 0));
            setMousePosition(SetDataActivity.nownum);
            //累计热量
            String allhotStr = alldata_analysis(recv_byte, 4, p + 17, 1, 1);
            String allhotStrNum = "", allhotStrUnit = "";
            for (int i = 0; i < allhotStr.length() - 2; i++) {
                if (allhotStr.substring(i, i + 2).equals("  ")) {
                    allhotStrNum = allhotStr.substring(0, i);
                    allhotStrUnit = allhotStr.substring(i + 2, allhotStr.length());
                }
            }
            //SetDataActivity.allhot.setText(allhotStrNum);
            //SetDataActivity.allhotunit.setText(allhotStrUnit);
            //累计流量
            String allflowStr = alldata_analysis(recv_byte, 4, p + 37, 1, 1);
            String allflowStrNum = "", allflowStrUnit = "";
            for (int i = 0; i < allflowStr.length() - 2; i++) {
                if (allflowStr.substring(i, i + 2).equals("  ")) {
                    allflowStrNum = allflowStr.substring(0, i);
                    allflowStrUnit = allflowStr.substring(i + 2, allflowStr.length());
                }
            }
            //SetDataActivity.allflow.setText(allflowStrNum);
            SetDataActivity.allflowunit.setText(allflowStrUnit);
            //所有时间
            SetDataActivity.alltime.setText(alldata_analysis(recv_byte, 3, p + 47, 0, 0));
            setMousePosition(SetDataActivity.alltime);
            SetDataActivity.rcvtext.setText(
                    "当前表号: " + alldata_analysis(recv_byte, 4, p + 5, 0, 0) + "\n" +
                            "累计热量: " + alldata_analysis(recv_byte, 4, p + 22, 1, 1) + "\n" +
                            "累计流量: " + alldata_analysis(recv_byte, 4, p + 37, 1, 1) + "\n" +
                            "累计时间: " + alldata_analysis(recv_byte, 3, p + 47, 0, 0) + "  h");
        } else {
            SetDataActivity.rcvtext.setText("表未返回数据,如下是调试工具返回!");
        }// alldata_analysis(recv_byte,4, p+22, 1, 1)+"\n"//(recv_byte,4, p+17, 1, 1)
    }

    /**
     * @param recv_byte 数据设置-设置
     */
    public static void SetDataSetBtn(byte[] recv_byte) {
        int p = checkRcvData(recv_byte, (byte) 0x68);//判断recv_byte[]中是否包含a 和 b
        //接收报文
        SetDataActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));
        //接收解析
        if (p > 0) {
            SetDataActivity.rcvtext.setText(
                    "当前表号:" + alldata_analysis(recv_byte, 4, p + 5, 0, 0) + "\n" +
                            "累计热量:" + alldata_analysis(recv_byte, 4, p + 17, 1, 1) + "\n" +
                            "累计流量:" + alldata_analysis(recv_byte, 4, p + 37, 1, 1) + "\n" +
                            "累计时间:" + alldata_analysis(recv_byte, 3, p + 47, 0, 0)
            );
        } else {
            SetDataActivity.rcvtext.setText("请确认是否修改成功!");
        }

    }

    /**
     * @param recv_byte 流量修正-读取
     */
    public static void flowRead(byte[] recv_byte) {
        int p = checkRcvData(recv_byte, (byte) 0x68);//判断recv_byte[]中是否包含a 和 b
        //接收报文
        FlowActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));
        //接收解析
        byte[] a = new byte[10];
        for (int i = 1; i < 10; i++) {
            a[i] = recv_byte[p + 46 + i];
        }
        String flowByte = BaseActivity.byteToHexString(a);
        String value1 = flowTrans(recv_byte[p + 46], recv_byte[p + 47], (byte) 0x00);
        String value2 = flowTrans(recv_byte[p + 48], recv_byte[p + 49], (byte) 0x00);
        String value3 = flowTrans(recv_byte[p + 50], recv_byte[p + 51], (byte) 0x00);
        String value4 = flowTrans(recv_byte[p + 52], recv_byte[p + 53], (byte) 0x00);
        String value5 = flowTrans(recv_byte[p + 54], recv_byte[p + 55], recv_byte[p + 55 + 6]);

        if (value1.equals("null") && (value2.equals("null"))) {
            FlowActivity.rcvtext.setText(flowByte + "\n" +
                    "读取失败! ");
            FlowActivity.row11.setText("");
            FlowActivity.row12.setText("");
            FlowActivity.row13.setText("");
            FlowActivity.row14.setText("");
            FlowActivity.row15.setText("");
        } else {
            FlowActivity.rcvtext.setText(flowByte + "\n" +
                    "补偿值一: " + value1 + "\n" +
                    "补偿值二: " + value2 + "\n" +
                    "补偿值三: " + value3 + "\n" +
                    "补偿值四: " + value4 + "\n" +
                    "补偿值五: " + value5);
            FlowActivity.row11.setText(value1);
            FlowActivity.row12.setText(value2);
            FlowActivity.row13.setText(value3);
            FlowActivity.row14.setText(value4);
            FlowActivity.row15.setText(value5);
        }

    }

    /**
     * @param recv_byte 流量修正-设置
     */
    public static void flowSet(byte[] recv_byte) {
        int p = checkRcvData(recv_byte, (byte) 0x68);//判断recv_byte[]中是否包含a 和 b
        //接收报文
        FlowActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));
        //接收解析
        byte[] a = new byte[10];
        for (int i = 1; i < 10; i = i + 2) {
            a[i - 1] = recv_byte[p + 46 + i];
            a[i] = recv_byte[p + 46 + i - 1];
        }
        String flowByte = BaseActivity.byteToHexString(a);

        FlowActivity.rcvtext.setText("请确认是否修改成功!");
        /* FlowActivity.rcvtext.setText(flowByte+"\n"
            +"修改前一: " +flowTrans(recv_byte[p+46],recv_byte[p+47],(byte)0x00)+"\n"
            +"修改前二: " +flowTrans(recv_byte[p+48],recv_byte[p+49],(byte)0x00)+"\n"
            +"修改前三: " +flowTrans(recv_byte[p+50],recv_byte[p+51],(byte)0x00)+"\n"
            +"修改前四: " +flowTrans(recv_byte[p+52],recv_byte[p+53],(byte)0x00)+"\n"
            +"修改前五: " +flowTrans(recv_byte[p+54],recv_byte[p+55],recv_byte[p+55+4])+"\n"
            +"请重新读取一遍!");*/
    }

    /**
     * 将接收到的流量点字节转换成数值
     *
     * @return
     */
    private static String flowTrans(byte a1, byte a2, byte last) {
        double value1 = 0, value2 = 0;
        if (a1 < 0) {

            value1 = 256 + a1;
        } else {
            value1 = 0 + a1;
        }
        Log.e("lyl", "a1" + a1);
        if (a2 < 0) {
            value2 = 256 + a2;
        } else {
            value2 = 0 + a2;
        }

        double value = value2 * 256 + value1;

        value = ((409600.0 / value) - 100);

        value = (double) ((int) (value * 1000)) / 1000;//保留两位小数

        //value ? 4point  : 5point
        String result = (value > 1000) ? "null" : Double.toString(value);
        Log.e("lyl", "result" + result);
        return result;
    }

    /**
     * 将接收到的流量点字节转换成数值
     *
     * @return
     */
    public static void inCheck(byte[] recv_byte) {

        int p = checkRcvData(recv_byte, (byte) 0x68);//判断recv_byte[]中是否包含a 和 b
        //接收报文
        CheckActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));
        //接收解析
        CheckActivity.rcvtext.setText("请确认是否成功进入检定状态!");
    }

    /**
     * runa modbus协议抄表解析
     */
    public static void runamodbus(byte[] rcv_byte) {

        int p = checkRcvData(rcv_byte, (byte) 0x68);

        RunaModusActivity.rcvbyte.setText(BaseActivity.byteToHexString(rcv_byte, p));

        //p=p+21;

        if (p > 0) {
            RunaModusActivity.rcvtext.setText(
                    "地        址: " + Integer.toString(rcv_byte[p] & 0xFF).toUpperCase() + "\n"
                            + "瞬时流量: " + getData_runamodbus(rcv_byte, p + 3, 4, 10000) + "  m³/h" + "\n"
                            + "累计流量: " + getData_runamodbus(rcv_byte, p + 7, 4, 1) + "  m³" + "\n"
                            + "瞬时热量: " + getData_runamodbus(rcv_byte, p + 11, 4, 100) + "  kW" + "\n"
                            + "累计热量: " + getData_runamodbus(rcv_byte, p + 15, 4, 100) + "  MWh" + "\n"
                            + "累计冷量: " + getData_runamodbus(rcv_byte, p + 19, 4, 100) + "  MWh" + "\n"
                            + "供水温度: " + getData_runamodbus(rcv_byte, p + 23, 2, 100) + "  ℃" + "\n"
                            + "回水温度: " + getData_runamodbus(rcv_byte, p + 25, 2, 100) + "  ℃" + "\n"
            );
        } else {
            RunaModusActivity.rcvtext.setText("表未返回数据,如下是调试工具返回!");
        }

    }

    /**
     * runa modbus返回数据解析
     */
    private static String getData_runamodbus(byte[] rcv_byte, int start, int len, int point) {
        String strresult = "";
        long result = 0;
        for (int i = 0; i < len; i++) {
            if (i == (len - 4))
                result = result + (1l * (rcv_byte[start + i] & 0xFF) * 256 * 256 * 256l);
            if (i == (len - 3)) result = result + (1l * (rcv_byte[start + i] & 0xFF) * 256 * 256l);
            if (i == (len - 2)) result = result + (1l * (rcv_byte[start + i] & 0xFF) * 256l);
            if (i == (len - 1)) result = result + (1l * (rcv_byte[start + i] & 0xFF) * 1l);
        }
        int z = (int) (result / point);
        int x = (int) (result % point);
        strresult = z + "." + x;

        return strresult;
    }

    /**
     * 其他公司类型超标接收解析
     */
    public static void otherCompanyRcv(byte[] rcv_byte) {

        int p = checkRcvData(rcv_byte, (byte) 0x68);

        if (p == 0)
            OtherCompanyActivity.rcvtext.setText("表未返回数据,如下是调试工具返回!");
        else
            OtherCompanyActivity.rcvtext.setText("简单模式,未调试解析,请参考接收报文!");

        OtherCompanyActivity.rcvbyte.setText(BaseActivity.byteToHexString(rcv_byte, p));
    }

    /**
     * 欧标类型超标接收解析
     */
    public static void europeCompanyRcv(byte[] rcv_byte) {

        int p = checkRcvData(rcv_byte, (byte) 0x68);

        if (p == 0)
            EuropeCompanyActivity.rcvtext.setText("表未返回数据,如下是调试工具返回!");
        else
            EuropeCompanyActivity.rcvtext.setText("简单模式,未调试解析,请参考接收报文!");

        EuropeCompanyActivity.rcvbyte.setText(BaseActivity.byteToHexString(rcv_byte, p));
    }

    /**
     * 汇中公司类型超标接收解析
     */
    public static void huizhongCompanyRcv(byte[] rcv_byte) {

        int p = checkRcvData(rcv_byte, (byte) 0x68);

        if (p == 0)
            HuizhongActivity.rcvtext.setText("表未返回数据,如下是调试工具返回!");
        else
            HuizhongActivity.rcvtext.setText("简单模式,未调试解析,请参考接收报文!");

        HuizhongActivity.rcvbyte.setText(BaseActivity.byteToHexString(rcv_byte, p));
    }

    /**
     * 读取电量解析
     */
    public static void readPwr(byte[] rcv_byte) {
        int p = checkRcvData(rcv_byte, (byte) 0x68);
        int readpwr = (byte) (((rcv_byte[22] >> 4) * 16) + (rcv_byte[22] & 0x0F));
        RudeActivity.pwr.setText(String.valueOf(readpwr) + "%");
    }

    public static void readPwrSimple(byte[] rcv_byte) {
        int p = checkRcvData(rcv_byte, (byte) 0x68);
        int readpwr = (byte) (((rcv_byte[22] >> 4) * 16) + (rcv_byte[22] & 0x0F));
        RudeSimpleActivity.pwr.setText(String.valueOf(readpwr) + "%");
    }

    public static void readPwrModbus(byte[] rcv_byte) {
        int p = checkRcvData(rcv_byte, (byte) 0x68);
        int readpwr = (byte) (((rcv_byte[22] >> 4) * 16) + (rcv_byte[22] & 0x0F));
        RudeRunaModbusActivity.pwr.setText(String.valueOf(readpwr) + "%");
    }

    /**
     * Runa_modbus_修改表号
     */
    public static void runamodbusNum(byte[] rcv_byte) {

        int p = checkRcvData(rcv_byte, (byte) 0x68);

        //p=p+21 ;
        if (p == 0)
            RunaModusNumActivity.rcvtext.setText("表未返回数据,如下是调试工具返回!");
        else
            RunaModusNumActivity.rcvtext.setText("地        址: " + Integer.toString(rcv_byte[p] & 0xFF).toUpperCase() + "");


        RunaModusNumActivity.rcvbyte.setText(BaseActivity.byteToHexString(rcv_byte, p));
    }


    /***************************************瑞纳阀门解析*************************************************************/

       /*读取阀门抄表解析：runa188*/
    public static void runaValveReadMeterRcv(byte[] recv_byte) {

        //1.判断rcv是否符合接收格式 return:p是0x68的位置
        int p = checkRcvData(recv_byte, (byte) 0x68);//判断recv_byte[]中是否包含a 和 b
        Log.e("lylp", "runaValveReadMeterRcv: " + p);
        //显示所有接收到的字节 中 表返回
        com.runachina.runavalve.ReadMeterActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));

        if (p > 0) {
            //判断温度锁定
            String temLock = alldata_analysis(recv_byte, 3, p + 49, 0, 0);
            if (temLock.equals("000000")) {
                temLock = "未锁定";
            } else {
                temLock = "锁定";
            }

            //提示
            String warn, warn1, warn2, warn3;
            if (alldata_analysis(recv_byte, 1, p + 56, 0, 0).equals("00")) {
                warn = "不在温控器液晶屏上提示充值";
            } else {
                warn = "在温控器液晶屏上提示充值";
            }
            if (alldata_analysis(recv_byte, 1, p + 57, 0, 0).equals("00")) {
                warn1 = "不在温控器液晶屏上提示欠费";
            } else {
                warn1 = "在温控器液晶屏上提示欠费";
            }
            if (alldata_analysis(recv_byte, 1, p + 58, 0, 0).equals("00")) {
                warn2 = "不在温控器液晶屏上显示热量值";
            } else {
                warn2 = "在温控器液晶屏上显示热量值";
            }
            if (alldata_analysis(recv_byte, 1, p + 59, 0, 0).equals("00")) {
                warn3 = "不在温控器液晶屏上显示开阀时间";
            } else {
                warn3 = "在温控器液晶屏上显示开阀时间";

            }
            //掉电阀门动作
            String elecAction = "";
            if (alldata_analysis(recv_byte, 1, p + 61, 0, 0).equals("00")) {
                elecAction = "不动作";
            } else if (alldata_analysis(recv_byte, 1, p + 61, 0, 0).equals("01")) {
                elecAction = "开阀";
            } else {
                elecAction = "关阀";
            }

            //日期时间
            String dataTime = "";
            dataTime = alldata_analysis(recv_byte, 7, p + 80, 0, 0);
            if (dataTime.length() == 14) {
                dataTime = dataTime.substring(0, 4) + "-" + dataTime.subSequence(4, 6) + "-" + dataTime.subSequence(6, 8) + " "
                        + dataTime.substring(8, 10) + ":" + dataTime.substring(10, 12) + ":" + dataTime.substring(12, 14);
            } else {
                dataTime = alldata_analysis(recv_byte, 7, p + 80, 0, 0);
            }


            Log.e("89", "runaValveReadMeterRcv: " + alldata_analysis(recv_byte, 1, p + 89, 0, 0));

            com.runachina.runavalve.ReadMeterActivity.rcvtext.setText(
                    //2.解析表号并显示
                    "表        号:  " + alldata_analysis(recv_byte, 4, p + 5, 0, 0) + "\n"
                            + "分摊热量:  " + alldata_analysis(recv_byte, 6, p + 29, 0, 1) + "kwh" + "\n"
                            + "上报开关:  " + (alldata_analysis(recv_byte, 1, p + 34, 0, 0).equals("00") ? "温控器和阀之间60分钟通讯一次" : "正常按上报周期通讯") + "\n"
                            + "上报周期:  " + Integer.parseInt(alldata_analysis(recv_byte, 1, p + 35, 0, 0), 16) + "min" + "\n"
                            + "阀门锁定:  " + alldata_analysis(recv_byte, 2, p + 45, 0, 0) + "\n"
                            + "温度锁定:  " + temLock + "\n"

                            + "温度上下限:  " + alldata_analysis(recv_byte, 2, p + 51, 0, 1) + "  ℃" +
                            " ~ " + alldata_analysis(recv_byte, 2, p + 53, 0, 1) + "  ℃" + "\n"
                            + "温度补偿:  " + Integer.parseInt(alldata_analysis(recv_byte, 1, p + 54, 0, 0), 16) * 0.1 + "  ℃" + "\n"
                            + "启动温差:  " + Integer.parseInt(alldata_analysis(recv_byte, 1, p + 55, 0, 0), 16) * 0.1 + "  ℃" + "\n"

                            + warn + "\n"
                            + warn1 + "\n"
                            + warn2 + "\n"
                            + warn3 + "\n"
                            + "掉电阀门动作:  " + elecAction + "\n"
                            + "进水温:  " + alldata_analysis(recv_byte, 2, p + 71, 0, 1) + "  ℃" + "\n"
                            + "回水温:  " + alldata_analysis(recv_byte, 2, p + 73, 0, 1) + "  ℃" + "\n"
                            + "日期时间:  " + dataTime + "\n"
                            + "版本号:  " + alldata_analysis(recv_byte, 1, p + 90, 0, 0) + "\n"
                            + "当前温度:  " + alldata_analysis(recv_byte, 2, p + 99, 0, 1) + "  ℃" + "\n"
                            + "设定温度:  " + alldata_analysis(recv_byte, 2, p + 101, 0, 1) + "  ℃" + "\n"
                            + "无线通讯次数:  " + alldata_analysis(recv_byte, 1, p + 102, 0, 0) + "\n"

            );
        } else {
            com.runachina.runavalve.ReadMeterActivity.rcvtext.setText("表未返回数据,如下是调试工具返回!");
        }

    }


    //恢复出厂设置
    public static void runaValveFactoryResetRcv(byte[] recv_byte) {
        //1.判断rcv是否符合接收格式 return:p是0x68的位置
        int p = checkRcvData(recv_byte, (byte) 0x68);//判断recv_byte[]中是否包含a 和 b
        Log.e("lylp", "runaValveReadMeterRcv: " + p);
        //显示所有接收到的字节 中 表返回
        com.runachina.runavalve.FactoryResetActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));
        if (p > 0) {
            com.runachina.runavalve.FactoryResetActivity.rcvtext.setText("请确认是否修改成功!");
        }
    }


    //数据清零
    public static void runaValveDataResetRcv(byte[] recv_byte) {
        //1.判断rcv是否符合接收格式 return:p是0x68的位置
        int p = checkRcvData(recv_byte, (byte) 0x68);//判断recv_byte[]中是否包含a 和 b
        Log.e("lylp", "runaValveReadMeterRcv: " + p);
        //显示所有接收到的字节 中 表返回
        com.runachina.runavalve.DataResetAvtivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));
        if (p > 0) {
            com.runachina.runavalve.DataResetAvtivity.rcvtext.setText("请确认是否修改成功!");
        }
    }

    //硬件复位
    public static void runaValveHardwareResetRcv(byte[] recv_byte) {
        //1.判断rcv是否符合接收格式 return:p是0x68的位置
        int p = checkRcvData(recv_byte, (byte) 0x68);//判断recv_byte[]中是否包含a 和 b
        Log.e("lylp", "runaValveReadMeterRcv: " + p);
        //显示所有接收到的字节 中 表返回
        com.runachina.runavalve.HardwareResetActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));
        if (p > 0) {
            com.runachina.runavalve.HardwareResetActivity.rcvtext.setText("请确认是否修改成功!");
        }
    }

    //修改表号 读取
    public static void runaValveMofifyReadNumRcv(byte[] recv_byte) {
        //1.判断rcv是否符合接收格式 return:p是0x68的位置
        int p = checkRcvData(recv_byte, (byte) 0x68);//判断recv_byte[]中是否包含a 和 b
        Log.e("lylp", "runaValveReadMeterRcv: " + p);

        //显示所有接收到的字节 中 表返回
        com.runachina.runavalve.ModifyNumActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));

        if (p > 0) {
            com.runachina.runavalve.ModifyNumActivity.rcvtext.setText(
                    //2.解析表号并显示
                    "表        号:  " + alldata_analysis(recv_byte, 4, p + 5, 0, 0)
            );
            com.runachina.runavalve.ModifyNumActivity.oldnum.setText(
                    //2.解析表号并显示
                    alldata_analysis(recv_byte, 4, p + 5, 0, 0)
            );
        } else {
            com.runachina.runavalve.ModifyNumActivity.rcvtext.setText("表未返回数据,如下是调试工具返回!");
        }
    }

    //修改表号 设置
    public static void runaValveMofifySetNumRcv(byte[] recv_byte) {
        //1.判断rcv是否符合接收格式 return:p是0x68的位置
        int p = checkRcvData(recv_byte, (byte) 0x68);//判断recv_byte[]中是否包含a 和 b
        Log.e("lylp", "runaValveReadMeterRcv: " + p);
        //显示所有接收到的字节 中 表返回
        com.runachina.runavalve.ModifyNumActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));
        //修改成功
        if (p > 0) {
            com.runachina.runavalve.ModifyNumActivity.oldnum.setText("");
            com.runachina.runavalve.ModifyNumActivity.newnum.setText("");
            com.runachina.runavalve.ModifyNumActivity.rcvtext.setText("请确认是否修改成功!");
        }
    }

    /**************************************亿林阀门解析*************************************************/

     /*读取阀门抄表解析：亿林阀门*/
    public static void yilinValveReadMeterRcv(byte[] recv_byte) {

        //1.判断rcv是否符合接收格式 return:p是0x68的位置
        int p = checkRcvData(recv_byte, (byte) 0x68);//判断recv_byte[]中是否包含a 和 b
        Log.e("lylp", "runaValveReadMeterRcv: " + p);
        //显示所有接收到的字节 中 表返回
        com.runachina.yilinvalve.YiLinValveReadMeterActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));

        System.out.println();
        //启动温差 bit4--bit6
        String temDiff = alldata_analysisForYilin(recv_byte, 3, p + 4, 7, 4);
        temDiff = Integer.toString(Integer.valueOf(temDiff, 2));

        //手动设定温度  bit0 -- bit5
        String setTem = alldata_analysisForYilin(recv_byte, 3, p + 6, 6, 1);
        setTem = Integer.toString(Integer.valueOf(setTem, 2));


        if (p > 0) {
            Log.e("temAdd", "yilinValveReadMeterRcv: " + alldata_analysis(recv_byte, 1, p, 0, 0));
            //判断是否以50开头
            if (alldata_analysis(recv_byte, 1, p, 0, 0).equals("50")) {
                //补偿温度 如果得到的十六进制转化成十进制大于127 则是带负号应该减去256
                String temAdd = "";
                temAdd = alldata_analysis(recv_byte, 1, p + 4, 0, 0);
                Log.e("temAdd", "yilinValveReadMeterRcv: " + temAdd);
                if (Integer.valueOf(temAdd, 16) > 127) {
                    temAdd = String.valueOf(Integer.valueOf(temAdd, 16) - 256);
                } else {
                    temAdd = String.valueOf(Integer.valueOf(temAdd, 16));
                }

                Log.e("data5", "yilinValveReadMeterRcv: " + alldata_analysis(recv_byte, 1, p + 8, 0, 0));
                Log.e("data5", "yilinValveReadMeterRcv: " + alldata_analysis(recv_byte, 1, p + 9, 0, 0));
                Log.e("data5", "yilinValveReadMeterRcv: " + alldata_analysis(recv_byte, 1, p + 10, 0, 0));
                com.runachina.yilinvalve.YiLinValveReadMeterActivity.rcvtext.setText(
                        //2.解析表号并显示
                        "表        号:  " + alldata_analysis(recv_byte, 2, p + 2, 0, 0) + "\n"
                                + "状        态:  " + (alldata_analysisForYilin(recv_byte, 3, p + 4, 1, 0).equals("0") ? "关机" : "开机") + "\n"
                                + "强制开启负载标志:  " + (alldata_analysisForYilin(recv_byte, 3, p + 4, 2, 1).equals("0") ? "负载正常工作" : "负载强制开启") + "\n"
                                + "锁定面板:  " + (alldata_analysisForYilin(recv_byte, 3, p + 4, 3, 2).equals("0") ? "解锁" : "锁定") + "\n"
                                + "启动温差:  " + temDiff + "  ℃" + "\n"
                                + "强制关闭负载标志:  " + (alldata_analysisForYilin(recv_byte, 3, p + 4, 8, 7).equals("0") ? "负载正常" : "负载强制关闭") + "\n"
                                + "补偿温度:  " + temAdd + "  ℃" + "\n"
                                + "手动设定温度:  " + setTem + "  ℃" + "\n"
//                                + "充值标志:  " + (alldata_analysisForYilin(recv_byte, 3, p + 6, 7, 6).equals("0") ? "未充值" : "已充值") + "\n"
//                                + "欠费标志:  " + (alldata_analysisForYilin(recv_byte, 3, p + 6, 8, 7).equals("0") ? "未欠费" : "已欠费") + "\n"

                                + "温度突变标志:  " + (alldata_analysisForYilin(recv_byte, 3, p + 7, 1, 0).equals("0") ? "温度正常" : "温度突变") + "\n"
                                + "电池电压状态:  " + (alldata_analysisForYilin(recv_byte, 3, p + 7, 2, 1).equals("0") ? "电池电压正常" : "电池电压低") + "\n"
                                + "负载状态:  " + (alldata_analysisForYilin(recv_byte, 3, p + 8, 1, 0).equals("0") ? "负载关闭" : "负载开启") + "\n"
                                + "无线故障:  " + (alldata_analysisForYilin(recv_byte, 3, p + 8, 2, 1).equals("0") ? "无线正常" : "无线故障") + "\n"
                                + "房间温度:  " + Integer.valueOf(alldata_analysis(recv_byte, 1, p + 8, 0, 0), 16) + "  ℃" + "\n"
                                + "进水温度:  " + Integer.valueOf(alldata_analysis(recv_byte, 1, p + 9, 0, 0), 16) + "  ℃" + "\n"
                                + "回水温度:  " + Integer.valueOf(alldata_analysis(recv_byte, 1, p + 10, 0, 0), 16) + "  ℃" + "\n"
                );
            } else {
                com.runachina.yilinvalve.YiLinValveReadMeterActivity.rcvtext.setText("接收信息错误!");
            }
        } else {
            com.runachina.yilinvalve.YiLinValveReadMeterActivity.rcvtext.setText("表未返回数据,如下是调试工具返回!");
        }

    }

    /**
     * 亿林阀门解析
     *
     * @param many     向前解析字节数
     * @param position 开始解析位置
     * @param start    截取起始位置
     * @param end      截取结束位置
     */

    public static String alldata_analysisForYilin(byte[] recv_byte, int many, int position, int start, int end) {
        //解析成字符串P
        String rcvStr = "", transStr;

        rcvStr = byteToBit(recv_byte[position - 1]);

        rcvStr = rcvStr.substring(rcvStr.length() - start, rcvStr.length() - end);

        return rcvStr;
    }

    /**
     * 把byte转为字符串的bit
     */
    public static String byteToBit(byte b) {
        return ""
                + (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1)
                + (byte) ((b >> 5) & 0x1) + (byte) ((b >> 4) & 0x1)
                + (byte) ((b >> 3) & 0x1) + (byte) ((b >> 2) & 0x1)
                + (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1);
    }
    /************************************************瑞纳自主温度采集器*****************************************************/
    /**
     * 瑞纳自主温度采集器
     * 读取数据
     *
     * @param recv_byte
     */
    public static void runaTemCollReadMeter(byte[] recv_byte) {
        //1.判断rcv是否符合接收格式 return:p是0x68的位置
        int p = checkRcvData(recv_byte, (byte) 0x68);//判断recv_byte[]中是否包含a 和 b
        Log.e("lylp", "runaValveReadMeterRcv: " + p);
        //显示所有接收到的字节 中 表返回
        com.runachina.temcollector.TemReadMeterActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));
        //修改成功

        if (p > 0) {
            if (BaseActivity.byteToHexString(recv_byte, p).startsWith("68")) {
                //2.解析表号并显示
                com.runachina.temcollector.TemReadMeterActivity.rcvtext.setText(
                        "表        号:  " + alldata_analysis(recv_byte, 4, p + 5, 0, 0)

                );
            }else{
                com.runachina.temcollector.TemReadMeterActivity.rcvtext.setText("接收信息错误!");
            }
        } else {
            ReadMeterActivity.rcvtext.setText("表未返回数据,如下是调试工具返回!");
        }
    }

    // 修改表号 读取表号
    public static void runaReadCollModifyMeterNum(byte[] recv_byte) {
        //1.判断rcv是否符合接收格式 return:p是0x68的位置
        int p = checkRcvData(recv_byte, (byte) 0x68);//判断recv_byte[]中是否包含a 和 b
        Log.e("lylp", "runaValveReadMeterRcv: " + p);
        //显示所有接收到的字节 中 表返回
        com.runachina.temcollector.TemModifyNumActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));
        //修改成功

        if (p > 0) {
            if (BaseActivity.byteToHexString(recv_byte, p).startsWith("68")) {
                //2.解析表号并显示
                com.runachina.temcollector.TemModifyNumActivity.rcvtext.setText(
                        "表        号:  " + alldata_analysis(recv_byte, 4, p + 5, 0, 0)
                );
                TemModifyNumActivity.oldnum.setText(
                        alldata_analysis(recv_byte, 4, p + 5, 0, 0));
            } else {
                ReadMeterActivity.rcvtext.setText("表未返回数据,如下是调试工具返回!");
            }
        } else {
            ReadMeterActivity.rcvtext.setText("表未返回数据,如下是调试工具返回!");

        }
    }

    //设置  修改表号
    public static void runaSetCollModifyMeterNum(byte[] recv_byte) {
        //1.判断rcv是否符合接收格式 return:p是0x68的位置
        int p = checkRcvData(recv_byte, (byte) 0x68);//判断recv_byte[]中是否包含a 和 b
        Log.e("lylp", "runaValveReadMeterRcv: " + p);
        //显示所有接收到的字节 中 表返回
        com.runachina.temcollector.TemModifyNumActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));
        //修改成功
        if (p > 0) {
            com.runachina.temcollector.TemModifyNumActivity.oldnum.setText("");
            com.runachina.temcollector.TemModifyNumActivity.newnum.setText("");
            com.runachina.temcollector.TemModifyNumActivity.rcvtext.setText("请确认是否修改成功!");
        } else {
            com.runachina.temcollector.TemModifyNumActivity.rcvtext.setText("表未返回数据,如下是调试工具返回!");
        }
    }


    /**
     * 硬件复位  数据清零  恢复出厂
     *
     * @param recv_byte
     */
    public static void runaTemDataResetRcv(byte[] recv_byte, int type) {
        //1.判断rcv是否符合接收格式 return:p是0x68的位置
        int p = checkRcvData(recv_byte, (byte) 0x68);//判断recv_byte[]中是否包含a 和 b
        Log.e("lylp", "runaValveReadMeterRcv: " + p);
        if (p > 0) {
            if (type == 1) {
                //显示所有接收到的字节 中 表返回
                com.runachina.temcollector.TemFactoryResetActivity.rcvtext.setText("请确认是否修改成功!");
                com.runachina.temcollector.TemFactoryResetActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));
            } else if (type == 2) {
                com.runachina.temcollector.TemDataResetActivity.rcvtext.setText("请确认是否修改成功!");
                com.runachina.temcollector.TemDataResetActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));
            } else {
                com.runachina.temcollector.TemHardwareResetActivity.rcvtext.setText("请确认是否修改成功!");
                com.runachina.temcollector.TemHardwareResetActivity.rcvbyte.setText(BaseActivity.byteToHexString(recv_byte, p));
            }

        } else {
            if (type == 1) {
                //显示所有接收到的字节 中 表返回
                com.runachina.temcollector.TemFactoryResetActivity.rcvtext.setText("表未返回数据,如下是调试工具返回!");
            } else if (type == 2) {
                com.runachina.temcollector.TemDataResetActivity.rcvtext.setText("表未返回数据,如下是调试工具返回!");
            } else {
                com.runachina.temcollector.TemHardwareResetActivity.rcvtext.setText("表未返回数据,如下是调试工具返回!");
            }
        }
    }
}
